package com.networkmanagerj
{
		
	
	import com.networkmanagerj.IDispose;
	
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.KeyboardEvent;
	import flash.events.TextEvent;
	import flash.ui.Keyboard;
	
	import mx.controls.TextInput;
	import mx.managers.IFocusManagerComponent;
	import mx.utils.StringUtil;

	public class XvicTextInput extends TextInput implements IFocusManagerComponent,IDispose
	{
		private var _validationFault : Boolean;
		private var charArray        : Array;
		private var mastText         : String;
		private var backSpaceDown    : Boolean;
        private var isMask           : Boolean;
        			
		private function onFocusEnter(event : FocusEvent):void
		{
		   if(event.type == FocusEvent.FOCUS_IN)
		   {
		      this.setSelection(0,this.textField.text.length+1);
		   }	
		}			
		
		private function keyDownEvent(event : KeyboardEvent) : void
		{			
			if(event.keyCode == Keyboard.BACKSPACE)
			   this.backSpaceDown = true;
			else
			   this.backSpaceDown = false;			
		}		
			
		private function onTextChange(event : Event):void
		{	
			
			if(!this.backSpaceDown)
			{
				var str1 : String = "";
   			    var str2 : String = "";

				for(var j : uint = 0;j < charArray.length; j++)
				{
					this.textField.text = StringUtil.substitute(this.textField.text,charArray[j].caractere,"");				
				}			


				for(var i : uint = 0;i < charArray.length; i++)
				{  

					str1 = this.textField.text.substr(0,charArray[i].intervalo);
					str2 = this.textField.text.substr(charArray[i].intervalo,this.textField.text.length)	
					this.textField.text =  str1 + charArray[i].caractere + str2;

				}
			}
			
			if(this.isMask)
			   this.setSelection(this.textField.text.length+1,this.textField.text.length+1);
			 
			
						
			
			/*
			for (var i:uint=0; i < charArray.length; i++)
			{
				if (!this.backSpaceDown)
				{
					if (len == charArray[i].intervalo)
					{
						this.textField.text+=charArray[i].caractere;
						this.setSelection(charArray[i].intervalo + 1, charArray[i].intervalo + 1);
					}
					else if (len == 1 && charArray[i].intervalo == 0)
					{
						this.textField.text=charArray[i].caractere + this.textField.text;
						this.setSelection(charArray[i].intervalo + 2, charArray[i].intervalo + 2);
					}
				}
			}*/		  
		}


		public function XvicTextInput()
		{
			super();
			this.charArray = new Array();
			this.mastText = "";
			this.backSpaceDown = false;
			this.isMask = false;
			
			this.addEventListener(KeyboardEvent.KEY_DOWN,keyDownEvent);
			this.addEventListener("textChanged",onTextChange);			
			this.addEventListener(FocusEvent.FOCUS_IN,onFocusEnter);		
			
			this.createChildren();	
		}
		
		public function dispose():void
		{
			this.removeEventListener(KeyboardEvent.KEY_DOWN,keyDownEvent);
			this.removeEventListener(TextEvent.TEXT_INPUT,onTextChange);
			this.removeEventListener(FocusEvent.FOCUS_IN,onFocusEnter);								
		}		
		
		
		override public function set errorString(value:String):void
		{
		   super.errorString = value;
		   if(value == null || value == "")
		      this._validationFault = false;
		   else
		      this._validationFault = true;   
		      	
		}
		
		public function get validationFault():Boolean
		{
			return this._validationFault;
		}
		
		[Inspectable(defaultValue="AlfaNumerico",enumeration="Data,Hora,CPF,CNPJ,CEP,Telefone,Placa,Numerico,AlfaNumerico",category="Common")]
		public function set maskType(type : String):void
		{
		   this.isMask  = true;
		   this.restrict = "0-9";
		   	
		   switch(type.toUpperCase())
		   {
		      case "DATA":
		      {
		      	this.mastText = "##/##/####"; 
		      	break;
		      }
		      case "HORA":
		      {
		      	this.mastText = "##:##";
		      	break;
		      }
		      case "TELEFONE":
		      {
		         this.mastText = "(##)####-####"; 
		         break;
		      }
			  case "CPF":
		      {
		         this.mastText = "###.###.###-##"; 
		         break;
		      }
			  case "CNPJ":
		      {
		         this.mastText = "##.###.###/####-##"; 
		         break;
		      }
			  case "CEP":
		      {
		         this.mastText = "#####-###"; 
		         break;
		      }
			  case "PLACA":
		      {
		      	 this.restrict = null;
		         this.mastText = "###-####"; 
		         break;
		      }   		
		      case "NUMERICO":
		      {
		      	this.mastText = "";
		      	this.isMask = false;
		      	break;
		      }         		
		      case "ALFANUMERICO":
		      {
		      	this.restrict = null;
		      	this.mastText = "";
		      	this.isMask = false;
		      	break;
		      }      
		      
		   }
		   
		    if(isMask)
		    {		    	
				this.maxChars = this.mastText.length;
				for(var i:uint =0;i<mastText.length;i++)
			    {
			    	if(mastText.charAt(i) != '#')
			    	   charArray.push({intervalo:i,caractere:mastText.charAt(i)});
			    }
			}		   
		    
		   this.invalidateDisplayList();		  	  
		}		
		
	}
}